###########################################################################
#
#   Copyright 2010 American Public Media Group
#
#   This file is part of AIR2.
#
#   AIR2 is free software: you can redistribute it and/or modify
#   it under the terms of the GNU General Public License as published by
#   the Free Software Foundation, either version 3 of the License, or
#   (at your option) any later version.
#
#   AIR2 is distributed in the hope that it will be useful,
#   but WITHOUT ANY WARRANTY; without even the implied warranty of
#   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#   GNU General Public License for more details.
#
#   You should have received a copy of the GNU General Public License
#   along with AIR2.  If not, see <http://www.gnu.org/licenses/>.
#
###########################################################################

package AIR2::DBManager;
use strict;
use warnings;
use Carp;
use Data::Dump qw( dump );
use AIR2::Config;
use base 'Rose::DBx::AutoReconnect';

my $cfg      = AIR2::Config::get_db_ini();
my $user     = AIR2::Config::get_user();
my $hostname = AIR2::Config::get_hostname();
my $profile  = AIR2::Config::get_profile_name();

for my $section ( $cfg->Sections ) {
    __PACKAGE__->register_db(
        domain            => $section,
        type              => $user,
        driver            => $cfg->val( $section, 'driver' ),
        database          => $cfg->val( $section, 'dbname' ),
        host              => $cfg->val( $section, 'hostname' ),
        username          => $cfg->val( $section, 'username' ),
        password          => $cfg->val( $section, 'password' ),
        post_connect_sql  => ['SET NAMES utf8'],
        mysql_enable_utf8 => 1,
        server_time_zone  => $cfg->val( $section, 'server_time_zone' ),
    );
}

__PACKAGE__->default_domain( $ENV{AIR2_DOMAIN} || $profile || $hostname || 'dev' );
__PACKAGE__->default_type($user);

1;

